package com.it.bigdata.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import sun.management.resources.agent;

import java.io.IOException;
import java.io.InputStream;

public class HttlClient {
   /**
    * @param  url 下载的页面的url
    * @return  页面内容html字符串
    */
    public static String getHtml(String url)  {
        //创建http客户端对象
       HttpClient httlClient= HttpClients.createDefault();
        HttpGet httpGet=new HttpGet(url);
        String [] userAgent={"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
                "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0",
                "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)"};

        int s=(int)(Math.random()*4);
        String agent=userAgent[s];
        httpGet.setHeader("User-Agent",agent);
       //对传入的url执行操作，返回响应数据包
        HttpResponse execute = null;
        try {
            execute = httlClient.execute(httpGet);
            //返回响应数据包中的响应实体
            HttpEntity entity = execute.getEntity();
            //将响应实体转换为字符串返回
            String html = EntityUtils.toString(entity);
            return html;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;    }

    public static InputStream getStream(String url)  {
        //创建http客户端对象
        HttpClient httlClient= HttpClients.createDefault();
        HttpGet httpGet=new HttpGet(url);

        //对传入的url执行操作，返回响应数据包
        HttpResponse execute = null;
        try {
            execute = httlClient.execute(httpGet);
            //返回响应数据包中的响应实体
            HttpEntity entity = execute.getEntity();
            //将响应实体转换输出流返回
            InputStream content = entity.getContent();
            return content;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
